package org.ytu.classifier.wavApi;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WavFileIO {
   public List<Double> readFrames(WavFile wavFile) {
      List<Double> allBuffer = new ArrayList<Double>();
      double[] buffer = new double[50001];

      int framesRead = 0;
      do {
         try {
            framesRead = wavFile.readFrames(buffer, 50000);
         } catch (Exception e) {
            System.out.println(e.getMessage());
         }
         for (int i = 0; i < framesRead; i++) {
            allBuffer.add(buffer[i]);
         }
      } while (framesRead != 0);
      return allBuffer;
   }

   public List<Double> readFrames(String fileName) {
      WavFile wavFile = null;
      List<Double> allBuffer;
      try {
         wavFile = WavFile.openWavFile(new File(fileName));
         allBuffer = new ArrayList<Double>();
         double[] buffer = new double[50001];

         int framesRead = 0;
         do {
            try {
               framesRead = wavFile.readFrames(buffer, 50000);
            } catch (Exception e) {
               System.out.println(e.getMessage());
            }
            for (int i = 0; i < framesRead; i++) {
               allBuffer.add(buffer[i]);
            }

         } while (framesRead != 0);
      } catch (Exception e) {
         System.out.println(e.getMessage());
         return null;
      } finally {
         try {
            wavFile.close();
         } catch (IOException e) {
            System.out.println(e.getMessage());
         }
      }
      return allBuffer;
   }
}
